'''
Author: @zhyy
Date: 2021-02-03 17:43:26
LastEditors: @zhyy
LastEditTime: 2021-02-03 18:48:17
'''
'''
Author: @zhyy
Date: 2021-02-03 17:07:51
LastEditors: @zhyy
LastEditTime: 2021-02-03 17:42:06
'''
from app import db
from app.dao.query_to_json import QueryToJson

DBSession=db.session

class DBQuery():
    @staticmethod
    def get_id(field,filters):
        '''
        description:查询表的id 
        param {*}
        return id 
        '''
        try:
            result=DBSession.query(field).filter(*filters).first()
        except Exception as e:
            return repr(e)
        if result:
            return result.id
        else:
            return None
       
    @staticmethod
    def get_one_use_tb(_cls,filters):
        try:
            result=DBSession.query(_cls).filter(*filters).first()
        except Exception as e:
            return repr(e)
        if result:
            data=QueryToJson.query_Single_table_to_json(result)
            return data
        else:
            return None
      

    @staticmethod
    def get_one_use_field(keys,field,filters):
        try:
            result=DBSession.query(*field).filter(*filters).first()
        except Exception as e:
            return repr(e)
        if result:
            data =QueryToJson.query_table_field_to_json(keys,result)
            return data
        else:
            return None
        
       
        
    @staticmethod
    def get_all_use_tb(_cls,filters):
        try:
            result=DBSession.query(_cls).filter(*filters).all()
        except Exception as e:
            return repr(e)
        if result:
            data=QueryToJson.query_Single_table_to_json_list(result)
            return data
        else:
            return None
       
    
       
    
    @staticmethod
    def get_all_use_field(keys,field,filters):
        try:
            result=DBSession.query(*field).filter(*filters).all()
        except Exception as e:
            return repr(e)
        if result:
            data=QueryToJson.query_table_field_to_json_list(keys,result)
            return data
        else:
            return None
      
    
    
    # 查询单表分页，返回json格式
    @staticmethod
    def get_page_use_tb(_cls,filters,page,per_page):
        '''
        description: 
        param {*}
        return {*}
        '''
        try:
            result = db.session.query(_cls).filter(*filters).paginate(
                        page=page, per_page=per_page, error_out=False)
        except Exception as e:
            return repr(e)
        if result:
            data={}
            for k,v in result.__dict__.items():
                if not k.startswith("query"):
                    data[k]=v
            item=QueryToJson.query_Single_table_to_json_list(data.get("items"))
            data["items"]=item
            return data
        else:
            return None
        
        
    # 查询表字段分页，返回json格式
    @staticmethod
    def get_page_use_field(keys_field,filters,page,per_page):
        '''
        description: 查询表字段分页，返回json格式
        param field={"uid":User.id,"role_id":Role.id}
        return {*}
        '''
        keys=list(keys_field.keys())
        field=list(keys_field.values())
        try:
            result = db.session.query(*field).filter(*filters).paginate(
                        page=page, per_page=per_page, error_out=False)
        except Exception as e:
            return repr(e)
        if result:
            data={}
            for k,v in result.__dict__.items():
                if not k.startswith("query"):
                    data[k]=v
            item=QueryToJson.query_table_field_to_json_list(keys,data.get("items"))
            data["items"]=item
            return data
        else:
            return None

        
